package com.aiit.ssm.service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.aspectj.weaver.reflect.ReflectionBasedResolvedMemberImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.aiit.ssm.dao.EmployeeDao;
import com.aiit.ssm.model.Department;
import com.aiit.ssm.model.Employee;
import com.aiit.ssm.utils.ExcelUtil;

@Service
public class EmployeeService {
	
	@Autowired
	EmployeeDao eDao;

	//保存员工
	public int save(Employee emp) {
		return eDao.save(emp);
	}
	
	//查询员工
	public List<Employee> findAllEmployee(){
		return eDao.findAllEmployee();
	}
	
	public Employee findEmployee(int id) {
		return eDao.findEmployee(id);
	}
	
	//修改员工
	public int updateEmployee(Employee employee) {
		
		return eDao.updateEmployee(employee);
	}
	//删除员工
	public int deleteEmployee(int id) {
		return eDao.deleteEmployee(id);
		
	}
	
	
	public int findEmpByName(String name) {
		return eDao.findEmpByName(name);
	}
	
	//查询员工总记录数
	public long findTotalRecords() {
		return eDao.findTotalRecords();
	}
	//导出
	public List<Employee> OutputExcel() {
		return eDao.findAllEmployee();
	}
	
	//自动补全和模糊搜索
	public List<Employee> searchAll(String name) {
		return eDao.searchEmp(name);
	}
	
	public ArrayList<String> searchName() {
		return eDao.searchEmpName();
	}
	
	
	
	//导入
	public String InputExcel(InputStream is, String originalFilename) {
		Map<String,Object> map = new HashMap<String,Object>();
		
		List<ArrayList<Object>> list;
		//if (originalFilename.endsWith(".xls")) {
			//list = ExcelUtil.readExcel2003(is);
		//} //else {
			list = ExcelUtil.readExcel2007(is);
		//}
		System.out.println("InputExcelService.list.size():"+list.size());
		System.out.println("InputExcelService.list.size():"+list);
		if(list.size()>0) {
			for (int i=0,j=list.size()-1;i<j;i++){		
			List<Object> row = list.get(i);	
			if(row!=null) {
			
				map.put("name", row.get(0).toString());
				map.put("gender", row.get(1).toString());
				map.put("email", row.get(2).toString());
				map.put("dept_id", row.get(3).toString());
		        eDao.InputExcel(map);  
		        //System.out.println("||map.size():"+map.size());
	        }
		}		
		
		}
		return "01";
	}

	
}
